<?php
namespace App\Http\Controllers\Order;

use App\Http\Controllers\Controller;
use App\Models\Order\PayWay;
use Illuminate\Http\Request;
use Storage;

class PayWayController extends Controller
{
    public function index(Request $request)
    {
        if ($request->ajax()) {
            
            $rows = $request->get("rows");
            
            $payway = $this->filters($request, new PayWay());
            
            $paginator = $payway->orderBy($request->get("sidx"), $request->get("sord"))->paginate($rows);
            
            $rows = [];
            foreach ($paginator as $page) {
                
                $rows[] = $page;
            }

            $data = [
                "rows" => $rows,
                "total" => $paginator->lastPage(),
                "page" => $paginator->currentPage(),
                "records" => $paginator->count(),
            ];
            
            return response()->json($data);
            
        } else {
            
            return view("order/payway/index");
        }
    }
    
    public function edit(Request $request)
    {
        $oper = $request->get("oper");
        
        if ($oper == "add"){
            
            $this->validate($request, [
                "is_show" => "integer|min:0|max:1",
            ]);
            
            if ($request->get("pay_name") == "银联"){
                if ($request->get("sort") > 1){
                    return response()->json(["success" => false , "message" => "银联支付的排序只能为0或1"]);
                }
            }

            $sort = PayWay::where("sort",$request->get("sort"))->count();
            
            if ($sort > 0){
                return response()->json(["success" => false , "message" => "排序不能重复"]);
            }

            $payway = new PayWay();
            $payway->pay_name    = $request->get("pay_name");
            $payway->api_type    = $request->get("api_type");
            $payway->pay_url     = $request->get("pay_url");
            $payway->method_type = $request->get("method_type");
            $payway->sort        = $request->get("sort");
            $payway->is_show     = $request->get("is_show");
            $payway->icon        = $request->get("icon");
            $payway->save();
            
        }elseif ($oper == "edit") {
            $this->validate($request, [
                "id" => "int"
            ]);

            if ($request->get("pay_name") == "银联"){
                if ($request->get("sort") > 1){
                    return response()->json(["success" => false , "message" => "银联支付的排序只能为0或1"]);
                }
            }
            
            $sort = PayWay::where("sort",$request->get("sort"))->where("id", "<>", $request->get("id"))->count();
            
            if ($sort > 0){
                return response()->json(["success" => false , "message" => "排序不能重复"]);
            }

            $payway = PayWay::find($request->get("id"));
            $payway->pay_name    = $request->get("pay_name");
            $payway->api_type    = $request->get("api_type");
            $payway->pay_url     = $request->get("pay_url");
            $payway->method_type = $request->get("method_type");
            $payway->sort        = $request->get("sort");
            $payway->is_show     = $request->get("is_show");
            $payway->icon        = $request->get("icon");
            $payway->save();
            
        }elseif ($oper == "del") {
            $payway = PayWay::find($request->get("id"));
            $payway->delete();
        } else {
            return response()->json(["success" => false , "message" => "oper error"]);
        }
        
        return response()->json(["success" => true]);
    }

}